﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.Specialized;

namespace GenSQL
{
    public partial class Form1 : Form
    {
        private string filePath = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenTextFile();
        }

        private void OpenTextFile()
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Title = "打开文本文件!";
            // 文件过滤类型
            openfile.Filter = "Text files (*.txt)|*.txt";
            // 缺省过滤文件类型
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                this.filePath = openfile.FileName;
            }
        }

        private void help_Click(object sender, EventArgs e)
        {
            MessageBox.Show("select sys_guid() from dual");
        }

        private void btnGen_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.filePath))
            {
                GenSQL genSQL = new GenSQL();
                NameValueCollection head = genSQL.ReadHeadByLine(filePath);
                string sql = head.Get("sql");
                string columns = head.Get("columns");
                string[] contents = genSQL.ReadContentByLine(filePath);
                List<string> list = genSQL.Gen(sql, columns, contents);
                string sq = "";
                foreach (string tmp in list)
                {
                    sq += tmp + "\n";
                }
                string outPutPath = filePath.Substring(0, filePath.LastIndexOf("\\") + 1);
                FileStream fs = new FileStream(outPutPath+"result.txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.Write(sq);
                sw.Flush();
                sw.Close();
                System.Diagnostics.Process.Start(outPutPath);
            }
            else 
            {
                MessageBox.Show("请选择数据文件！");
            }
        }
    }
}